﻿using System;
using System.Collections.Generic;
using RibbonLib;
using RibbonLib.Interop;

namespace todo.Ribbon
{
    public class RibbonGalleryControl : RibbonControl
    {
        public event Action<int> OnSelect;

        private List<GalleryItemPropertySet> _items;
        private int? _selectedIndex;

        public void FireOnSelect(int index)
        {
            var handler = OnSelect;
            if (handler != null) handler(index);
        }

        public int? SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                _selectedIndex = value;
                var uintValue = _selectedIndex.HasValue
                                        ? (uint) _selectedIndex.Value
                                        : Constants.UI_Collection_InvalidIndex;
                var vUintValue = PropVariant.FromObject(uintValue);
                var hr = _ribbonUI.Framework.SetUICommandProperty(_commandID,
                        ref RibbonProperties.SelectedItem, ref vUintValue);
            }
        }

        protected RibbonGalleryControl(RibbonUI ribbonUI, uint commandID)
                : base(ribbonUI, commandID)
        {
        }

        private IUICollection ItemsSource
        {
            get
            {
                PropVariant unknownValue;
                var hr = _ribbonUI.Framework.GetUICommandProperty(_commandID,
                        ref RibbonProperties.ItemsSource,
                        out unknownValue);
                if (NativeMethods.Succeeded(hr))
                {
                    return (IUICollection) unknownValue.Value;
                }
                return null;
            }
        }

        public override HRESULT UpdateProperty(ref PropertyKey key, PropVariantRef currentValue,
                                               ref PropVariant newValue)
        {
            if (key == RibbonProperties.ItemsSource)
            {
                UpdateItemsSource();
                return HRESULT.S_OK;
            }
            else if (key == RibbonProperties.SelectedItem)
            {
                if (_selectedIndex.HasValue)
                {
                    newValue.SetUInt((uint) _selectedIndex.Value);
                }
                else
                {
                    newValue.SetUInt(Constants.UI_Collection_InvalidIndex);
                }
            }
            return base.UpdateProperty(ref key, currentValue, ref newValue);
        }

        private void UpdateItemsSource()
        {
            var itemsSource = this.ItemsSource;
            itemsSource.Clear();
            if (_items == null || _items.Count == 0)
            {
                return;
            }
            foreach (var item in _items)
            {
                itemsSource.Add(item);
            }
        }

        public List<GalleryItemPropertySet> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public override HRESULT Execute(ExecutionVerb verb, PropertyKeyRef key,
                                        PropVariantRef currentValue,
                                        IUISimplePropertySet commandExecutionProperties)
        {
            if (verb == ExecutionVerb.Execute)
            {
                var index = (int) (uint) currentValue.PropVariant.Value;
                FireOnSelect(index);
            }
            return base.Execute(verb, key, currentValue, commandExecutionProperties);
        }
    }
}